import $C from './config.js';
import cryptojskey from './cryptojskey.js';
import cryptojs from './cryptojs.js';
export default {

	request(url, data, method) {
		data = this.get_data(data);
		return new Promise((res, rej) => {
			uni.request({
				url: this.get_url() + url,
				data: data,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				timeout: 30000,
				method: method,
				dataType: 'json',
				success: (result) => {
					if (result.data.encrypted) {
						var dataret = cryptojs.decrypt(result.data.data, cryptojskey.encrypted_key,
							cryptojskey.encrypted_iv);
						dataret = JSON.parse(dataret);
						dataret.status = result.data.status
						dataret.msg = result.data.msg


					} else {
						var dataret = result.data;
						dataret.status = result.data.status
						dataret.msg = result.data.status

					}

					if (result.data.system == 9000 || result.data.status == 9001 || result.data
						.status == 9002 || result.data
						.status == 401) {
						uni.showModal({
							title: '提示',
							content: result.data.msg,
							showCancel: false,
							success: function(res) {

							}
						});
						return
					}
					try {
						uni.setStorageSync('img_url', dataret.img_url);
						uni.setStorageSync('banmi_face', dataret.face);
						uni.setStorageSync('article_1', dataret.article_1);
						uni.setStorageSync('article_2', dataret.article_2);
						uni.setStorageSync('article_6', dataret.article_6);
						uni.setStorageSync('article_3', dataret.article_3);
					} catch (e) {

					}
					res(dataret)
				},
				fail: (error) => {
					return rej(error)
				}
			});


		})
	},
	post(url, data = {}) {
		return this.request(url, data, 'POST')
	},
	get(url, data) {
		return this.request(url, data, 'GET')
	},
	is_login() {
		if (!uni.getStorageSync('token')) {
			uni.navigateTo({
				url: '/pages/banmi/user_login'
			})
			return 0;
		}
		return 200;
	},
	get_data(data) {
		data.banmi_system = uni.getStorageSync('system');
		data.banmi_uniPlatform = uni.getStorageSync('uniPlatform');
		data.banmi_region = uni.getStorageSync('region');
		data.banmi_model = uni.getStorageSync('model');
		data.banmi_browserName = uni.getStorageSync('browserName');
		data.banmi_platform = uni.getStorageSync('platform');
		data.banmi_screenHeight = uni.getStorageSync('screenHeight');
		data.banmi_screenWidth = uni.getStorageSync('screenWidth');
		data.banmi_deviceBrand = uni.getStorageSync('deviceBrand');
		data.banmi_brand = uni.getStorageSync('brand');
		data.banmi_appVersion = uni.getStorageSync('appVersion');
		data.banmi_deviceModel = uni.getStorageSync('deviceModel');
		data.banmi_osAndroidAPILevel = uni.getStorageSync('osAndroidAPILevel');
		data.banmi_romName = uni.getStorageSync('romName');
		data.banmi_romVersion = uni.getStorageSync('romVersion');
		data.banmi_uid = uni.getStorageSync('uid');
		data.banmi_token = data.banmi_token ? data.banmi_token : uni.getStorageSync('token')
		data.invite_code = uni.getStorageSync('invite_code');
		data.lat = uni.getStorageSync('latitude');
		data.lng = uni.getStorageSync('longitude');
		var path = '';
		var pages = getCurrentPages()
		if (pages.length > 0) {
			let currentRoute = pages[pages.length - 1].route;
			path = currentRoute;
		}
		data.banmi_pages_path = path;
		data.banmi_banmi_plugin = $C.banmi_plugin;
		return data
	},
	get_url() {
		// #ifdef H5
		var domain = window.location.protocol + "//" + window.location.host + '/';
		console.log("process.env.NODE_ENV: " + JSON.stringify(process.env.NODE_ENV));
		if (process.env.NODE_ENV === 'development') { //开发环境 跨域
			$C.baseUrl = '/api/'
		}
		if (process.env.NODE_ENV === 'production') { //线上环境 当前域名
			$C.baseUrl = domain
		}
		// #endif
		console.log("$C.baseUrl: " + JSON.stringify($C.baseUrl));
		return $C.baseUrl
	},



}